package cola.rh.System.config;

import cola.rh.System.common.security.CustomMd5PasswordEncoder;
import cola.rh.System.filter.TokenAuthenticationFilter;
import cola.rh.System.filter.TokenLoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Name: WebSecurityConfig
 * @Author: Cola
 * @Time: 2023/3/9 23:20
 * @Description: WebSecurityConfig
 */
@Configuration
/*开启 SpringSecurity 默认行为*/
@EnableWebSecurity
/*开启 SpringSecurity 方法注解*/
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private CustomMd5PasswordEncoder customMd5PasswordEncoder;

    @Autowired
    private RedisTemplate redisTemplate;


    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                /*关闭csrf跨站请求伪造*/
                .csrf().disable()
                /*开启跨域以便前端调用接口*/
                .cors().and()
                .authorizeRequests()
                /*指定某些接口不需要通过验证即可访问。eg: 登陆接口*/
                .antMatchers("/v1/api/index/login").permitAll()
                /*其它所有接口需要认证才能访问*/
                .anyRequest().authenticated()
                .and()
                /*TokenAuthenticationFilter 放到 TokenLoginFilter 即 UsernamePasswordAuthenticationFilter 的前面，
                这样做就是为了除了登录的时候去查询数据库外，其他时候都用 token 进行认证。*/
                .addFilterBefore(new TokenAuthenticationFilter(redisTemplate), UsernamePasswordAuthenticationFilter.class)
                .addFilter(new TokenLoginFilter(authenticationManager(), redisTemplate));

        /*禁用session*/
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /*指定 UserDetailService 和 加密器*/
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(customMd5PasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     * 排除swagger相关请求
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/favicon.ico",
                "/swagger-resources/**",
                "/webjars/**",
                "/v2/**",
                "/swagger-ui.html/**", "/doc.html",
                "/actuator/**");
    }
}
